package org.dashboard.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import org.apache.poi.hssf.usermodel.HSSFDateUtil;
import org.apache.poi.ss.usermodel.DataFormatter;
import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.dashboard.bean.AdDataset;
import org.dashboard.bean.AdRole;
import org.dashboard.bean.AdUser;
import org.dashboard.bean.JsonResponse;
import org.dashboard.service.AdDatasetService;
import org.dashboard.service.AdIndexService;
import org.dashboard.service.ExcelUtilService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/index")
public class AdIndexController extends BaseController {

    private static final Logger logger = LoggerFactory.getLogger(AdIndexController.class);

    @Autowired
    private ExcelUtilService excelUtilService;

    @Reference
    private AdIndexService adIndexService;

    @Reference
    private AdDatasetService adDatasetService;

    @RequestMapping(value = "/chart/data", method = {RequestMethod.POST, RequestMethod.GET}, produces = MediaType.APPLICATION_FORM_URLENCODED_VALUE)
    public JsonResponse chartData(HttpServletRequest request) {
        String dataSetCodes = request.getParameter("dataSetCodes");
        String params = request.getParameter("params");

        AdUser user = currentAdUser(request);
        List<AdRole> roles = currentUserAdRoles(request);

        JSONArray dataSetCodesJsonArray = (JSONArray) JSON.parse(dataSetCodes);
        ArrayList<String> dataSetCodeList = new ArrayList<>();
        if (dataSetCodesJsonArray != null) {
            int len = dataSetCodesJsonArray.size();
            for (int i = 0; i < len; i++) {
                dataSetCodeList.add(dataSetCodesJsonArray.getString(i));
            }
        }

        JSONArray paramsJsonArray = (JSONArray) JSON.parse(params);
        ArrayList<Map<String, Object>> paramList = new ArrayList<>();
        if (paramsJsonArray != null) {
            int len = paramsJsonArray.size();
            for (int i = 0; i < len; i++) {
                paramList.add(paramsJsonArray.getJSONObject(i));
            }
        }

        JsonResponse jsonResponse = new JsonResponse();
        Map<String, Object> data = new HashMap<>();
        for (String dataSetCode : dataSetCodeList) {
            AdDataset adDataset = adDatasetService.selectAdDatasetByCode(dataSetCode);
            if (adDataset != null && !data.containsKey(dataSetCode)) {
                JSONObject dataSetDefine = (JSONObject) JSON.parse(adDataset.getDatasetData());
                if (dataSetDefine.getString("DataServiceType").equals("excel")) {
                    data.put(dataSetCode, excelUtilService.loadChartExcelData(adDataset));
                } else {
                    data.put(dataSetCode, adIndexService.loadChartData(adDataset, paramList, user, roles));
                }
            }
        }
        jsonResponse.setData(data);
        jsonResponse.setCode(1);
        jsonResponse.setResult("success");
        return jsonResponse;
    }


    @RequestMapping(value = "/page/params", method = {RequestMethod.POST, RequestMethod.GET}, produces = MediaType.APPLICATION_FORM_URLENCODED_VALUE)
    public JsonResponse pageParams(HttpServletRequest request) {
        Integer pageId = Integer.parseInt(request.getParameter("pageId"));

        JsonResponse jsonResponse = new JsonResponse();

        jsonResponse.setData(adIndexService.loadPageParams(pageId));
        jsonResponse.setCode(1);
        jsonResponse.setResult("success");
        return jsonResponse;
    }
}
